<?
	session_start();
	include('conn.php');
	error_reporting(E_all);
	$opt = $_REQUEST["q"];
	switch($opt)
	{
		case '1':
			$qry1 = "SELECT sd.pairid,s.stbno,c.cardno,sd.stbtypeid,sd.Notes, st.STBType, 
			case when sd.assigned = 'y' then 'yes' else 'no' end as assigned
			from STBDetails sd, STBtype st, stbTable s,cardTable c
			where sd.Active='1' and st.stbtypeid=sd.stbtypeid and sd.stbid=s.id and sd.cardid=c.id 
			order by pairid desc";
			$qry = mysql_query($qry1);
			$data = array();

			while($rows = mysql_fetch_array($qry))
			{
				$data[] = array
					(			
						"id" => $rows['pairid'],
						"num" => $rows['stbno'],						
						"card" => $rows['cardno'],						
						"type" => $rows['stbtypeid'],
						"typename" => $rows['STBType'],
						"assigned" => $rows['assigned'],
						"note" => $rows['Notes']
					);
			}
			echo json_encode($data);
			break;
		
		case '2':
			$qry1 = "SELECT * from STBtype where status = '1' order by STBType";
			$qry = mysql_query($qry1);
			$data = array();
			while($rows = mysql_fetch_array($qry))
			{
				$data[] = array
					(			
						"stbid" => $rows['stbtypeid'],
						"stbtype" => $rows['STBType']						
					);
			}
			echo json_encode($data);
			break;
		case '3':
			$qry1 = "SELECT stbno from stbTable where status = '0' and flag='free' order by id";
			$qry = mysql_query($qry1);
			$data = array();
			$cnt=0;
			while($rows = mysql_fetch_array($qry))
			{
				$data[$cnt] =  $rows['stbno'];
				$cnt++;
			}
			echo json_encode($data);
			break;
			
		case '4':
			$qry1 = "SELECT cardno from cardTable where status = '0' and flag='free' order by id";
			$qry = mysql_query($qry1);
			$data = array();
			$cnt=0;
			while($rows = mysql_fetch_array($qry))
			{
				$data[$cnt] =  $rows['cardno'];
				$cnt++;
			}			
			echo json_encode($data);
			break;
		
		default:
			echo "No option selected";
	}	
?>